from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

# def verify_code(request):
#     code = request.POST['code']
#     decrypted = private_key.decrypt(code, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)).decode()
#     if decrypted.isdigit() and len(decrypted) == 6:
#         # 验证码正确，返回成功
#         return JsonResponse({'success': True})
#     else:
#         # 验证码错误，返回失败
#         return JsonResponse({'success': False})
# from openpyxl import load_workbook
# import re
#
# # 打开 Excel 文件
# workbook = load_workbook('D:\开发需求\七库全书\批量导入\合作伙伴库\LIST.xlsx')
#
# # 选择要操作的工作表
# worksheet = workbook.active
#
# # 遍历每一行
# for row in worksheet.iter_rows(min_row=1):
#     # 获取要处理的单元格的值
#     cell_value = row[0].value
#
#     # 使用正则表达式分割字符串
#     pattern = r'^(\d{2,3})(.*)(\..*)$'
#     match = re.match(pattern, cell_value)
#
#     if match:
#         # 分别获取三列的值
#         column_a_value = match.group(1)
#         column_b_value = match.group(2)
#         column_c_value = '.' + match.group(3)
#         print(column_a_value,column_b_value,column_c_value)
#         # 将三列的值写入 Excel 中
#         row[1].value = int(column_a_value)
#         row[2].value = column_b_value
#         row[3].value = column_c_value
#
# # 保存修改后的 Excel 文件
# workbook.save('D:\开发需求\七库全书\批量导入\合作伙伴库\LISTnew.xlsx')

# import requests
# from bs4 import BeautifulSoup
#
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
#
# url = 'https://www.che168.com/china/tesla/'
# params = {'pvareaid': '101635', 'type': 'dealer', 'page': 1}
#
# response = requests.get(url, headers=headers, params=params)
# soup = BeautifulSoup(response.text, 'html.parser')
#
# total_pages = int(soup.find('div', class_='page').find_all('a')[-2].text)
# print('total pages:', total_pages)
#
# for page in range(1, total_pages + 1):
#     params['page'] = page
#     response = requests.get(url, headers=headers, params=params)
#     soup = BeautifulSoup(response.text, 'html.parser')
#
#     car_list = soup.find_all('div', class_='list-r-img-wrap')
#     print('page', page, 'car list length:', len(car_list))
#
#     for car in car_list:
#         model = car.find('p', class_='cx-name').text.strip()
#         buy_time = car.find('p', class_='cx-info').text.strip()
#         price = car.find('div', class_='price').find('em').text.strip()
#
#         print(model, buy_time, price)

import random

board = []

# 初始化地雷生成
def init_board():
    for i in range(10):
        board.append(["O"] * 10)

# 随机放置 n 个 mines
def random_mines(n):
    while n > 0:
        x = random.randint(0, 9)
        y = random.randint(0, 9)
        if board[x][y] == "O":
            board[x][y] = "M"
            n -= 1

# 设置雷周围数字
def set_nearby_mines():
    for i in range(10):
        for j in range(10):
            if board[i][j] == "M":
                continue
            board[i][j] = get_nearby_mines(i,j)

# 计算邻近方格的雷数
def get_nearby_mines(i, j):
    mines = 0
    for x in range(i-1, i+2):
        for y in range(j-1, j+2):
            if x < 0 or y < 0 or x > 9 or y > 9:
                continue
            if board[x][y] == "M":
                mines += 1
    return str(mines)

# 输出 棋盘
def print_board():
    for i in range(10):
        print(" ".join(board[i]))


init_board()
random_mines(10)
set_nearby_mines()
print_board()
